package com.zcr.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcr.pojo.Druginfo;
import com.zcr.service.DruginfoService;
import com.zcr.vo.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@RestController
@RequestMapping("/druginfo")
public class DruginfoController {

    @GetMapping("/index")
    public ModelAndView index(){
        return new ModelAndView("druginfo/druginfo");
    }

    @Resource
    private DruginfoService druginfoService;

    @GetMapping("/search")
    public Result search(String drugname,  Integer page, Integer limit){
        Page<Druginfo> druginfoPage= new Page<>(page,limit);
        IPage<Druginfo> druginfoIPage = null;
        try {
            druginfoIPage = druginfoService.searchByDrugname(drugname, druginfoPage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Result sucess = Result.sucess(druginfoIPage.getRecords());
        sucess.setCount(Integer.parseInt(String.valueOf(druginfoIPage.getTotal())));
        return sucess;
    }

    //添加药品基础信息
    @PostMapping("/addDruginfo")
    public Result addDruginfo(Druginfo druginfo){
        try {
            druginfoService.addDruginfo(druginfo);
            return Result.success("添加药品成功");
        }catch (Exception ex){
            return Result.fail("添加药品失败",ex);
        }
    }

    @PostMapping("/updareDruginfo")
    public Result updareDruginfo(Druginfo druginfo){
        try {
            druginfoService.updareDruginfo(druginfo);
            return Result.success("修改药品成功");
        }catch (Exception ex){
            return Result.fail("修改药品失败",ex);
        }
    }

    @PostMapping("/deleteDruginfo")
    public Result deleteDruginfo(Druginfo druginfo){
        try {
            druginfoService.deleteDruginfo(druginfo);
            return Result.success("删除药品成功");
        }catch (Exception ex){
            return Result.fail("删除药品失败",ex);
        }
    }

    @PostMapping("/deleteAllDruginfos")
    public Result deleteAllDruginfos(@RequestBody String[] ids){
        for (int i=0;i<ids.length;i++){
            System.out.println(ids[i]);
        }
        try {
            druginfoService.deleteAllDruginfo(ids);
            return Result.success("删除药品成功");
        }catch (Exception ex){
            return Result.fail("删除药品失败",ex);
        }
    }




}
